package com.xin.service.impl;

import com.xin.dao.BookDao;
import com.xin.dao.impl.BookDaoimpl;
import com.xin.pojo.Book;
import com.xin.pojo.Page;
import com.xin.service.BookService;

import java.util.List;

public class BookServiceimpl implements BookService {
    private BookDao bookDao=new BookDaoimpl();
    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void update(Book book) {
        bookDao.update(book);
    }

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBook() {
        return bookDao.queryBook();
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page=new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Integer pageTotalCount = bookDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        int pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize>0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        int begin=(page.getPageNo()-1)*page.getPageSize();
        System.out.println(begin);
        List<Book> books = bookDao.queryForPageItems(begin, pageSize);
        page.setItems(books);
        return page;
    }
    public Page<Book> page2(int pageNo,int pageSize){
        Page<Book> page = new Page<>();

        page.setPageSize(pageSize);
        Integer pageTotalCount = bookDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        int pageTotal = pageTotalCount / pageSize;

        if (pageTotalCount % pageSize>0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo); //里面有有修改非法数据，需要用到pageTotal
        int begin = (page.getPageNo() - 1) * page.getPageSize();
        List<Book> books = bookDao.queryForPageItems(begin, pageSize);
        page.setItems(books);
        return page;
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Book> page = new Page<>();

        page.setPageSize(pageSize);
        Integer pageTotalCount = bookDao.queryForPageTotalCount(min,max);
        page.setPageTotalCount(pageTotalCount);
        int pageTotal = pageTotalCount / pageSize;

        if (pageTotalCount % pageSize>0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo); //里面有有修改非法数据，需要用到pageTotal
        int begin = (page.getPageNo() - 1) * page.getPageSize();
        List<Book> books = bookDao.queryForPageItems(min,max,begin, pageSize);
        page.setItems(books);

        return page;
    }
}
